SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
 
IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonFirstName]'))
    DROP VIEW vw_PersonFirstName
GO
CREATE VIEW vw_PersonFirstName
AS
SELECT
       p.PersonID
      ,p.FirstName
FROM  [dbo].[Persons] p
GO

 
IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonDetails]'))
    DROP VIEW vw_PersonDetails
GO
CREATE VIEW vw_PersonDetails
AS
SELECT
       p.PersonID
      ,p.Title
      ,p.FirstName
      ,p.LastName
      ,a.AddressLine1
      ,a.AddressLine2
      ,a.AddressLine3
      ,a.AddressLine4
      ,a.Country
      ,a.PostalCode
      ,a.PhoneNumber
      ,a.Email
      ,p.EntryDate
FROM  [dbo].[Persons] p
INNER JOIN [dbo].[Address] a ON p.PersonID = a.PersonID
GO


IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonDetailsPost]'))
    DROP VIEW vw_PersonDetailsPost
GO
CREATE VIEW vw_PersonDetailsPost
AS
SELECT
       p.PersonID
      ,p.Title
      ,p.FirstName
      ,p.LastName
      ,a.AddressLine1
      ,a.AddressLine2
      ,a.AddressLine3
      ,a.AddressLine4
      ,a.Country
      ,a.PostalCode
FROM  [dbo].[Persons] p
INNER JOIN [dbo].[Address] a ON p.PersonID = a.PersonID
GO


IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonTransactions]'))
    DROP VIEW vw_PersonTransactions
GO
CREATE VIEW vw_PersonTransactions
AS
SELECT
       p.PersonID
      ,t.TransactionID
      ,r.TransactionReceiptDetailID
      ,t.TransactionDate
      ,[dbo].GetPersonName(p.Title, p.FirstName, p.LastName) AS PersonName
      ,[dbo].GetModeName(t.TransactionModeID) AS ModeName
      ,t.TransactionModeNumber AS ModeNumber
      ,t.TransactionModeBank AS ModeBank
      ,t.TransactionModeDate AS ModeDate
      ,[dbo].[GetDonationAccountName](r.DonationAccountID) AS Account
      ,r.TransactionReceiptNumber AS ReceiptNumber
      ,r.TransactionReceiptDate AS ReceiptDate
      ,[dbo].[GetPostalModeName](r.PostalModeID) AS SentBy
      
FROM  [dbo].[Persons] p
INNER JOIN [dbo].[Transactions] t ON p.PersonID = t.PersonID
INNER JOIN [dbo].[TransactionReceiptDetail] r ON t.TransactionID = r.TransactionID
GO